using System;

namespace kinokasse.bauteile.contracts.ebcpatterns
{
    public interface IJoin<TInput0, TInput1>
    {
        void In0(TInput0 input0);
        void In1(TInput1 input1);
        event Action<Tuple<TInput0, TInput1>> Out_Joined;
    }

    public class Join<TInput0, TInput1> : IJoin<TInput0, TInput1>
    {
        enum States
        {
            Start,
            Input0,
            Input1,
        }

        private States state = States.Start;

        private TInput0 input0;
        private TInput1 input1;


        public void In0(TInput0 input0)
        {
            this.input0 = input0;
            if (this.state == States.Input1)
                JoinInputs();
            else
                this.state = States.Input0;
        }

        public void In1(TInput1 input1)
        {
            this.input1 = input1;
            if (this.state == States.Input0)
                JoinInputs();
            else
                this.state = States.Input1;
        }


        private void JoinInputs()
        {
            this.state = States.Start;
            this.Out_Joined(new Tuple<TInput0, TInput1>(this.input0, this.input1));
        }


        public event Action<Tuple<TInput0, TInput1>> Out_Joined;
    }
}